home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / ULARN.ARJ / ULARN.TAR / ularn / nap.c < prev    next >
C/C++ Source or Header  |  1989-10-25  |  2KB  |  106 lines

  1. /* nap.c */
  2.  
  3. #include "header.h"
  4.  
  5. /*
  6.  *    routine to take a nap for n milliseconds
  7.  */
  8. nap(x)
  9. register int x;
  10. {
  11.     if (x<=0) return; /* eliminate chance for infinite loop */
  12.     lflush();
  13.     if (x > 999) 
  14.         sleep(x/1000); 
  15. #ifndef M_XENIX            /* Xenix does not have times() */
  16.     else napms(x);
  17. #endif /* M_XENIX */
  18. }
  19.  
  20. #ifdef SYSV
  21. #ifndef M_XENIX
  22. /*    napms - sleep for time milliseconds - uses times() */
  23. /* this assumes that times returns a relative time in 60ths of a second */
  24. /* this will do horrible things if your times() returns seconds! */
  25. napms(time)
  26. int time;
  27. {
  28.     long matchclock, times();
  29.     struct tms stats;
  30.  
  31.     if (time<=0) time=1; /* eliminate chance for infinite loop */
  32.     if ((matchclock = times(&stats)) == -1 || matchclock == 0)
  33.         return;    /* error, or BSD style times() */
  34.     matchclock += (time / 17); /*17 ms/tic is 1000 ms/sec / 60 tics/sec */
  35.     while(matchclock > times(&stats))
  36.         ;
  37. }
  38. #endif /* M_XENIX */
  39.  
  40. #else /* not SYSV */
  41. #  ifdef BSD
  42. #    ifdef SIGVTALRM
  43.        /* This must be BSD 4.2!  */
  44. #      define bit(_a) (1<<((_a)-1))
  45. nullf()
  46. { }
  47.  
  48. /*    napms - sleep for time milliseconds - uses setitimer() */
  49. napms(time)
  50. int time;
  51. {
  52.     struct itimerval    timeout;
  53.     int     (*oldhandler) ();
  54.     int     oldsig;
  55.  
  56.     if (time <= 0) return;
  57.  
  58.     timerclear(&timeout.it_interval);
  59.     timeout.it_value.tv_sec = time / 1000;
  60.     timeout.it_value.tv_usec = (time % 1000) * 1000;
  61.  
  62.     oldsig = sigblock(bit(SIGALRM));
  63.     setitimer(ITIMER_REAL, &timeout, (struct itimerval *)0);
  64.     oldhandler = signal(SIGALRM, nullf);
  65.     sigpause(oldsig);
  66.     signal(SIGALRM, oldhandler);
  67.     sigsetmask(oldsig);
  68. }
  69.  
  70. #    else /* SIGVTALARM */
  71. /*    napms - sleep for time milliseconds - uses ftime() */
  72.  
  73. napms(time)
  74. int time;
  75. {
  76.     /* assumed to be BSD UNIX */
  77.     struct timeb _gtime;
  78.     time_t matchtime;
  79.     unsigned short matchmilli;
  80.     register struct timeb *tp = & _gtime;
  81.  
  82.     if (time <= 0) return;
  83.     ftime(tp);
  84.     matchmilli = tp->millitm + time;
  85.     matchtime  = tp->time;
  86.     while (matchmilli >= 1000) {
  87.         ++matchtime;
  88.         matchmilli -= 1000;
  89.     }
  90.  
  91.     while(1) {
  92.         ftime(tp);
  93.         if ((tp->time > matchtime) ||
  94.             ((tp->time == matchtime) && (tp->millitm >= matchmilli)))
  95.             break;
  96.     }
  97. }
  98. #    endif /* SIGVTALARAM */
  99. #  else not BSD
  100. napms(time) 
  101. int time; 
  102. {
  103. }    /* do nothing, forget it */
  104. #  endif /* BSD */
  105. #endif /* SYSV */
  106.